package cn.cupk.logsystem.controller;

import cn.cupk.logsystem.config.LogConfig;
import cn.cupk.logsystem.log.Level;
import cn.cupk.logsystem.log.factoryPattern.ConsoleHandler;
import cn.cupk.logsystem.log.observerPattern.Logger;
import cn.cupk.logsystem.log.singletonpattern.LogManager;
import cn.cupk.logsystem.log.strategypattren.SimpleFormatter;
import cn.cupk.logsystem.pojo.User;
import cn.cupk.logsystem.result.Result;
import cn.cupk.logsystem.service.UserService;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    private static final Logger LOGGER = LogManager.getInstance().getLogger(UserController.class.getName());

    @Autowired
    private UserService userService;

//    @Autowired
//    private LogConfig logConfig;
    @PostConstruct
    public void init() {
        // 设置控制台日志处理器
        ConsoleHandler consoleHandler = new ConsoleHandler();
        consoleHandler.setFormatter(new SimpleFormatter());
        LOGGER.addHandler(consoleHandler);

        // 设置默认或用户选择的日志处理器
//        Handler handler = HandlerFactory.createHandler(logConfig.getType());
//        if (handler != null) {
//            handler.setFormatter(new SimpleFormatter());
//            LOGGER.addHandler(handler);
//        }

        LOGGER.setLevel(Level.ALL);
    }
    @PostMapping("/add")
    public Result<String> createUser(@RequestBody User user) {
        try {
            userService.createUser(user);
            LOGGER.info("Created user: " + user);
            return Result.success("User created successfully");
        } catch (Exception e) {
            LOGGER.severe("Failed to create user: " + e.getMessage());
            return Result.error("Failed to create user: " + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public Result<User> getUserById(@PathVariable Long id) {
        try {
            User user = userService.getUserById(id);
            if (user != null) {
                LOGGER.info("Retrieved user: " + user);
                return Result.success(user);
            } else {
                LOGGER.warning("User not found with id: " + id);
                return Result.error("User not found");
            }
        } catch (Exception e) {
            LOGGER.severe("Error retrieving user: " + e.getMessage());
            return Result.error("Error retrieving user: " + e.getMessage());
        }
    }

    @GetMapping
    public Result<List<User>> getAllUsers() {
        try {
            List<User> users = userService.getAllUsers();
            LOGGER.info("Retrieved all users: " + users);
            return Result.success(users);
        } catch (Exception e) {
            LOGGER.severe("Error retrieving users: " + e.getMessage());
            return Result.error("Error retrieving users: " + e.getMessage());
        }
    }

    @PutMapping
    public Result<String> updateUser(@RequestBody User user) {
        try {
            userService.updateUser(user);
            LOGGER.info("Updated user: " + user);
            return Result.success("User updated successfully");
        } catch (Exception e) {
            LOGGER.severe("Failed to update user: " + e.getMessage());
            return Result.error("Failed to update user: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Result<String> deleteUser(@PathVariable Long id) {
        try {
            userService.deleteUser(id);
            LOGGER.info("Deleted user with id: " + id);
            return Result.success("User deleted successfully");
        } catch (Exception e) {
            LOGGER.severe("Failed to delete user: " + e.getMessage());
            return Result.error("Failed to delete user: " + e.getMessage());
        }
    }
}
